package ru.galiauir.gui;

import java.awt.Container;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.event.WindowAdapter;

import javax.swing.JFrame;
import javax.swing.JProgressBar;

public class ProgressForm {
	private static final int FRAMEWIDTH = 400;
	private static final int FRAMEHEIGHT = 70;
	
	private JFrame frame;
	private Container cp;
	private JProgressBar progressBar;
	
	public ProgressForm(int maxDays) {
		frame = new JFrame("Progress...");
		frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		frame.setSize(FRAMEWIDTH, FRAMEHEIGHT);
		frame.setResizable(false);
		setFrameLocation(frame);
		frame.addWindowListener(new WindowAdapter(){});
		
		createObjects(maxDays);
		initObjects();
		addObjects();
		
		frame.setVisible(true);
	}
	
	private void setFrameLocation(JFrame frm) {
		Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
		int left = ((int) (dim.getWidth() - frm.getWidth()) / 2);
		int top = ((int) (dim.getHeight() - frm.getHeight()) / 3);
		frm.setLocation(left, top);
	}
	
	private void createObjects(int maxDays) {
		progressBar = new JProgressBar(0, maxDays);
		progressBar.setStringPainted(true);
		progressBar.setBorderPainted(true);
	}
	
	private void initObjects() {
		progressBar.setBounds(10, 10, FRAMEWIDTH - 20, FRAMEHEIGHT - 50);
		progressBar.setVisible(true);
	}
	
	private void addObjects() {		
		cp = frame.getContentPane();
		cp.setLayout(null);
		cp.add(progressBar);
	}
	
	public void reRender(int day) {
		progressBar.setValue(day);
		cp.repaint();
	}
	
	public void dispose() {
		frame.dispose();
	}
	
}
